<template>
  <div class="energy-data-box">
    <el-form ref="form" :model="formData" :rules="rules" label-width="120px" label-suffix=":">
      <template v-for="(item, index) in formList">
        <el-form-item
          :key="`${item.prop}${index}`"
          :style="item.itemStyle || {}"
          :label="item.name"
          :prop="item.prop"
          :class="item.itemClass"
        >
          {{ formData[item.prop] }}
        </el-form-item>
      </template>
    </el-form>
  </div>
</template>

<script>
import { dictMixin } from '@/mixin/dict'
import { parseSceneData } from '@/utils/scene-data'
import roomApi from '@/api/apis/sceneRoom'

export default {
  name: 'OperateEnergyDataDetail',
  mixins: [dictMixin],
  props: {
    // 请求参数
    requestParams: {
      type: Object,
      default: () => ({})
    },
    // 弹窗数据
    popData: {
      type: Object,
      default: () => ({})
    },
    // 弹窗配置
    popConfig: {
      type: Object,
      default: () => ({})
    },
    // 弹窗取消
    close: {
      type: Boolean,
      default: false
    },
    // 弹窗确认
    confirm: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      isAdd: true,
      formData: {},
      formList: [
        {
          name: '所属项目',
          prop: 'projectName'
        },
        {
          name: '所属场景',
          prop: 'sceneInfoName'
        }, {
          name: '设备编码',
          prop: 'deviceCode'
        }, {
          name: '设备名称',
          prop: 'deviceName'
        }, {
          name: '统计产品类型',
          prop: 'productTypeName'
        },
        {
          name: '读表类型',
          prop: 'readMeterTypeName'
        },
        {
          name: '统计类型',
          prop: 'staticsTypeName'
        }, {
          name: '测算范围',
          prop: 'calculationRangeName'
        },
        {
          name: '能源名称',
          prop: 'energyTypeName'
        }, {
          name: '能源分类',
          prop: 'energyCategoryName'
        }, {
          name: '读表标识',
          prop: 'readMeterTagName'
        }, {
          name: '计量值',
          prop: 'meterValue'
        }, {
          name: '读表值',
          prop: 'readValue'
        },
        {
          name: '单位',
          prop: 'energyTypeUnit'
        }, {
          name: '标准煤（kgce）',
          prop: 'standardCoal'
        }, {
          name: '读取类型',
          prop: 'readTypeName'
        }
      ]
    }
  },
  computed: {
    rules() {
      return {
        meterValue: [{ required: true, message: '请输入计量值', trigger: 'change' }]
      }
    }
  },
  watch: {
    popData: {
      handler(data) {
        this.formData = Object.assign({}, data)
        console.log(this.formData)
        // const { popType } = this.popConfig
        // const dataId = ['add'].includes(popType) ? '' : data.id
        // this.isAdd = !dataId
        // this.formList.forEach((e) => {
        //   this.$set(this.formData, e.prop, data[e.prop])
        //   if (!this.isAdd && ['solidifyDate', 'sceneProjectId', 'meterDeviceId'].includes(e.prop)) {
        //     e.attrConfig.disabled = true
        //   }
        // })
        // this.$set(this.formData, 'energyTypeId', data.energyTypeId)
        // this.$set(this.formData, 'solidifyDate', data.solidifyDate)
        // this.$set(this.formData, 'id', dataId)
        // this.queryMeterDeviceByProjectId(data.sceneProjectId || '')
        // this.formData.sceneInfo = parseToSceneInfo(data)
        // if (data.sceneProjectId) {
        //   // 查询场景选择的数据
        //   this.getRoomList(data.sceneProjectId)
        // }
        // this.formList[2].optionList = [{ code: this.formData.productTypeId, name: data.productTypeName }]
        // console.log(this.formData, this.formList)
      },
      deep: true,
      immediate: true
    },
    confirm() {
      this.submit()
    }
  },
  methods: {
    async getRoomList(sceneProjectId) {
      const res = await roomApi.queryRoomList({
        sceneProjectId,
        sortColumn: 'floorName',
        sortOrder: 'asc'
      })
      const roomList = parseSceneData(res.data)
      const item = this.formList[1]
      item.attrConfig.options = roomList
      this.$set(this.formList, 1, { ...item })
    },
    /**
     * change事件
     */
    handlerSelectData(config) {
      const Ref = this.$refs[config.prop]
      if (Ref && config.isClosePop) {
        Ref.dropDownVisible = false
      }
      if (config.prop === 'sceneProjectId') {
        this.$set(this.formData, 'meterDeviceId', '')
        this.$set(this.formData, 'readMeterTagName', '')
        this.$set(this.formData, 'readMeterTypeName', '')
        this.$set(this.formData, 'energyTypeName', '')
        this.$set(this.formData, 'energyTypeUnit', '')
        this.$set(this.formData, 'energyTypeId', '')
        this.queryMeterDeviceByProjectId(this.formData[config.prop])
      }
      if (config.prop === 'meterDeviceId') {
        const meterObj = config.optionList.find(t => t.value === this.formData.meterDeviceId).baseObj
        if (meterObj !== null) {
          this.$set(this.formData, 'readMeterTagName', meterObj.readMeterTagName)
          this.$set(this.formData, 'readMeterTypeName', meterObj.readMeterTypeName)
          this.$set(this.formData, 'energyTypeName', meterObj.energyTypeName)
          this.$set(this.formData, 'energyTypeUnit', meterObj.energyTypeUnit)
          this.$set(this.formData, 'energyTypeId', meterObj.energyTypeId)
          this.querySceneInfo(meterObj.sceneEdificeId, meterObj.sceneFloorId, meterObj.sceneRoomId)
        }
      }
    },
    /**
     * 提交
     */
    submit() {
      this.$emit('onFinish', 'energy-data-detail')
    },
    /**
     * 通过项目Id查询计量设备
     * @param sceneProjectId
     * @returns {Promise<void>}
     */
    async queryMeterDeviceByProjectId(sceneProjectId) {
      const res = await this.getCommonSelecteds('MeterDeviceSelected', '', `{'sceneProjectId':'${sceneProjectId}'}`)
      const idx = this.formList.findIndex((e => e.prop === 'meterDeviceId'))
      const item = this.formList.find((e => e.prop === 'meterDeviceId'))
      const optionList = res.map(e => {
        return {
          ...e,
          code: e.value,
          name: e.label
        }
      }) || []
      this.$set(this.formList, idx, {
        ...item,
        optionList
      })
    },
    /**
     * 通过楼栋Id / 楼层Id / 空间Id 获取所属场景
     * @param sceneEdificeId
     * @param sceneFloorId
     * @param sceneRoomId
     * @returns {Promise<void>}
     */
    async querySceneInfo(sceneEdificeId, sceneFloorId, sceneRoomId) {
      const sceneInfo = await this.$api.energyData.querySceneInfo({
        sceneEdificeId,
        sceneFloorId,
        'id': sceneRoomId
      })
      this.$set(this.formData, 'sceneInfo', sceneInfo.data)
      console.log(this.formData)
    }
  }
}
</script>

<style lang="scss" scoped>
.energy-data-box {
  color: #333;
  padding: 0 20px;

  ::v-deep .el-form {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    .el-form-item {
      width: calc(50% - 10px);

      &.width100p {
        width: 100%;
      }

      .el-form-item__label {
        color: #666;
        font-size: 14px;
      }

      &.form-sub-title {
        width: 100%;

        .el-form-item__label {
          display: flex;
          align-items: center;
          justify-content: flex-start;
          font-weight: bold;
          color: #333;
          font-size: 16px;
          line-height: 1;

          &::before {
            content: '';
            display: inline-block;
            width: 4px;
            height: 15px;
            background-color: #00858d;
            margin-right: 5px;
          }
        }
      }
    }

    .el-select,
    .el-cascader {
      width: 100%;
    }
  }
}
</style>
